package com.lxyk.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageInfo;
import com.lxyk.mapper.SalaryMapper;
import com.lxyk.pojo.Salary;
import com.lxyk.service.SalaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;
@Service
public class SalaryServiceImpl implements SalaryService {
    @Autowired
    private SalaryMapper salaryMapper;

    @Override
    public PageInfo<Salary> calcSalaryByCondition(String startDate, String endDate, Integer pageNum, Integer pageSize) {
        //条件成立，表示“结束时间为空” 系统当前时间为结束时间 然后转换为字符串
        if (StrUtil.isEmpty(endDate)){//StrUtil是hutool的一个工具类
            Date date = new Date(System.currentTimeMillis());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            endDate=sdf.format(date);
        }
        List<Salary> salaryList = salaryMapper.calcSalaryByCondition(startDate, endDate);
        //工资降序(如果我们在sql中写的排序能展示出来就不用写这个 否则要写)
        Collections.sort(salaryList,(o1,o2)->{
            if (o1.getFinalSalary() > o2.getFinalSalary()){
                return -1;
            }else if (o1.getFinalSalary() < o2.getFinalSalary()){
                return 1;
            }else {
                return 0;
            }
        });
        return new PageInfo<>(salaryList);
    }

    @Override
    public PageInfo<Salary> calcSalary(Integer pageNum, Integer pageSize) {
        List<Salary> salaryList = salaryMapper.calcSalary();
        //工资降序
        Collections.sort(salaryList,(o1,o2) ->{
            if (o1.getFinalSalary() > o2.getFinalSalary()){
                return -1;
            }else if (o1.getFinalSalary() < o2.getFinalSalary()){
                return 1;
            }else {
                return 0;
            }
        });
        return new PageInfo<>(salaryList);
    }

    @Override
    public PageInfo<Salary> IsNullWorker(Integer pageNum, Integer pageSize) {
        List<Salary> salaryList = salaryMapper.IsNullWorker();
        //工资降序
        Collections.sort(salaryList,(o1,o2) ->{
            if (o1.getFinalSalary() > o2.getFinalSalary()){
                return -1;
            }else if (o1.getFinalSalary() < o2.getFinalSalary()){
                return 1;
            }else {
                return 0;
            }
        });
        return new PageInfo<>(salaryList);
    }



}
